package com.hx;

import com.hx.entity.Regions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import java.util.List;


public abstract class DBUtlis<T> {
    private static SessionFactory sessionFactory ;
    private static Session session;
    static {
        StandardServiceRegistry registry=new StandardServiceRegistryBuilder().
                configure().
                build();
         sessionFactory= new MetadataSources(registry).buildMetadata().buildSessionFactory();
         session=sessionFactory.getCurrentSession();
    }
    public abstract List<T> findAll();

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
     void ad(){
        System.out.println("asdf");
    }


    public int insert(T regions){
        try{
            session.beginTransaction();
            session.save(regions);
            session.getTransaction().commit();
            return 1;
        }catch (Exception e){
            session.getTransaction().rollback();
            return 0;
        }

    }
    public int delete(Integer integer){
        try {
            session.beginTransaction();
            Regions regions1=session.get(Regions.class,integer);
            session.delete(regions1);
            session.getTransaction().commit();
            return 1;
        }catch (Exception es){
            session.getTransaction().rollback();
            return 0;
        }
    }
    public int delete(T regions){
        try {
            session.beginTransaction();
            session.delete(regions);
            session.getTransaction().commit();
            return 1;
        }catch (Exception es){
            session.getTransaction().rollback();
            return 0;
        }
    }
    public int upData(T regions){
        try {
            session.beginTransaction();
            session.update(regions);
            session.getTransaction().commit();
            return 1;
        }catch (Exception es){
            session.getTransaction().rollback();
            return 0;
        }
    }

}
